<?php

/**
 * Msa_SlotObject - Classe jumelle à Msa_SlotObject, utilisée pour la gestion de la corbeille
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Msa_SlotObjectTrash extends Erra_Fields_Base_Object {

    function __construct($objectId, $buildArgs = null) {
   parent::__construct($objectId, $buildArgs);

        $event_id = Erra_Fields_Hidden::create('event_id')
                ->setInAdminList(true)
                ->setDatabaseType('VARCHAR', 128);

        $agenda_id = Erra_Fields_Hidden::create('agenda_id')
                ->setInAdminList(true)
                ->setDatabaseType('INT', 16);

        $event_types = array('presta' => 'Prestation', 'slot' => 'Plage horaire', 'web' => 'Ouverture Internet');
        $event_type = Erra_Fields_ComboBox::create('event_type')
                ->fill($event_types)
                ->setDatabaseType('VARCHAR', 8)
                ->setDescription("");

        $slot_start = Erra_Fields_Time::create('slot_start')
                ->setInAdminList(true)
                ->setDescription("Horaire de la prestation")
                ->setDatabaseType('INT', 16)
                ->append('<div class="fiche-prestation">');

        $slot_day = Erra_Fields_Date::create('slot_day')
                ->setInAdminList(true)
                ->setDatabaseType('INT', 16)
                ->setDescription("Date");

        //$prestationArray = Msa_PrestationObject::getPrestationTree();
        $prestation = Erra_Fields_Hidden::create('prestation')
                ->setDatabaseType('TEXT'); 

        // <DIV CLASS="fiche-prestation">
        $prestation_id = Erra_Fields_MultiLevelComboBox::create('prestation_id')
                ->setInAdminList(true)
                ->setDatabaseType('INT', 16)
                ->addAttribute('style', 'width:200px;');
        $this->addFields($event_id, $agenda_id, $event_type, $slot_day, $slot_start, $prestation_id);

        // Add extra fields here        
        Msa_ExtraPrestationFieldObject::addExtraFields($this);
        $this->addExtraFieldsIds();

        $client = Erra_Fields_Autocomplete::create('client')
                ->setDataSource("getdata/client_list")
                ->addAttribute('class', 'calendar-select-client lazy')
                ->setDatabaseType('NONE')
                ->setDescription('Client');


        $createClient = Msa_ClientObject::getObject(uniqid(), 'CREATE', null);
        $client->prepend($createClient->ajaxForm());


        $client_id = Erra_Fields_Hidden::create('client_id')
                ->addAttribute('class', 'hidden-client-id')
                ->setInAdminList(true)
                ->setDatabaseType('INT', 16)
                ->append('<div class="fiche-client">
                        <div class="loader"></div>
                    </div>');

        $status_rdv = Erra_Fields_ComboBox::create('status_rdv')
                ->setDescription('Statut')
                ->setDatabaseType('VARCHAR', 16)
                ->fill(array('en_attente' => 'En attente', 'venu' => 'Venu', 'pas_venu' => 'Pas venu'))
                ->append('</div>
                    <div class="plage-horaire">');

        $this->addFields($client, $client_id, $status_rdv);


        // </DIV>
        // <DIV CLASS="PLAGE-HORAIRE">
        // Plage horaire
        $title = Erra_Fields_Text::create('title')
                ->setDatabaseType('VARCHAR', 128)
                ->setDescription('Titre');
        $content = Erra_Fields_TextArea::create('content')
                ->setRows(3)
                ->setDatabaseType('TEXT')
                ->setDescription('Description');
        $slot_end = Erra_Fields_Time::create('slot_end')
                ->setDatabaseType('INT', 16)
                ->setDescription('Heure de fin');
        $slot_color = Erra_Fields_Color::create('slot_color')
                ->setDatabaseType('VARCHAR', 8)
                ->addAttribute('class', 'slot_color')
                ->setDescription('Couleur de la plage horaire');
        $repeat_enabled = Erra_Fields_MultipleCheckBox::create('repeat_enabled')
                ->fill(array('repeat' => 'Répéter ?'))
                ->append('<div class="repeat-infos">');
        // <DIV CLASS="repeat-infos">
        $occurency = Erra_Fields_Hidden::create('occurency');
        $deleted_occurencies = Erra_Fields_Hidden::create('deleted_occurencies');

        $repeat_time_data = array();
        $repeat_time_data_full_str = array();
        for ($i = 1; $i <= 12; $i++) {
            $repeat_time_data[$i] = $i;
            $repeat_time_data_full_str[$i] = $i . ' fois';
        }
        $repeat_time = Erra_Fields_ComboBox::create('repeat_time')
                ->setDescription('Tous les')
                ->setHelp('En choisissant "2", vous choisissez de répeter cette plage 1 jour sur 2 (ou une semaine sur 2, etc...')
                ->fill($repeat_time_data)
                ->prepend('<div class="inline-block">')
                ->setDatabaseType('TINYINT')
                ->append('</div>');

        $repeat_period_data = array('jour' => 'Jour', 'semaine' => 'Semaine', 'mois' => 'Mois', 'an' => 'An');
        $repeat_period = Erra_Fields_ComboBox::create('repeat_period')
                ->fill($repeat_period_data)
                ->setDatabaseType('VARCHAR', 16)
                ->prepend('<div class="inline-block">')
                ->append('</div>');

        $repeat_occurency = Erra_Fields_Radio::create('repeat_occurency')
                ->fill(array('always' => 'Toujours', 'specific_date' => "Jusqu'au"))
                ->setDatabaseType('VARCHAR', 16)
                ->setDefaultValue('always');

        $repeat_occurency_date = Erra_Fields_Date::create('repeat_occurency_date')
                ->setDatabaseType('VARCHAR', 32);

        $jours = array('monday' => 'Lundi', 'tuesday' => 'Mardi', 'wednesday' => 'Mercredi', 'thursday' => 'Jeudi', 'friday' => 'Vendredi', 'saturday' => 'Samedi', 'sunday' => 'Dimanche');
        $repeat_on_specific_days = Erra_Fields_MultipleCheckBox::create('repeat_on_specific_days')
                ->setDescription('Les jours suivants')
                ->fill($jours)
                ->addAttribute('class', 'specific_days')
                ->setDatabaseType('VARCHAR', 256)
                ->append('</div></div>');
        // </DIV> - repeat infos
        // </DIV> - plage horaire

        $from_internet = Erra_Fields_Hidden::create('from_internet')
                ->setDatabaseType('TINYINT')
                ->setValue(0);

        $date_commande = Erra_Fields_Hidden::create('date_commande')
                ->setDatabaseType('INT', 16)
                ->setValue(time());

        $paypal = Erra_Fields_Hidden::create('paypal')
                ->setDatabaseType('INT', 16)
                ->setValue(0);

        $last_sync = Erra_Fields_Hidden::create('last_sync')
                ->setDatabaseType('INT', 16)
                ->setValue(time());

        $this->addFields($slot_end);
        if ($buildArgs != 'web') {
            $this->addFields($title, $content);
        }
        $this->addFields($slot_color, $repeat_enabled, $occurency, $deleted_occurencies, $repeat_time, $repeat_period, $repeat_occurency, $repeat_occurency_date, $repeat_on_specific_days, $from_internet, $date_commande, $last_sync, $paypal, $prestation);
      
        
        
        $this->setDescription('Rendez-vous supprimés ces 3 derniers mois');
        $this->addAttribute('class', 'erra-slot-object modal');
        $this->setCrud('RD');
        $this->setOrderBy('slot_day DESC');
        $this->setRenderEngine('jqueryui');
        //$this->setSynchronized(false);
    }

    function addExtraFieldsIds() {
        // Get data
        for ($i = 1; $i <= 5; $i++) {
            if ($this->getSubFieldAt('extra_field_' . $i) == null) {
                $extra_field = Erra_Fields_Hidden::create('extra_field_' . $i);
                $this->addField($extra_field);
            }
        }
    }

    /**
     * postListInit() - Modification des champs pour les rendre lisibles dans la corbeille
     */
    function postListInit() {
        $list = $this->getObjectDataArray();
        foreach ($list as $key => $item) {
            $trashLine = Erra_Core_DatabasePdo::SELECT('SELECT nom_agenda, nom, prenom, prestation_name FROM msa_agendaobject, msa_clientobject, msa_prestationobject WHERE msa_agendaobject.id=' . $item['agenda_id'] . ' AND msa_clientobject.id=' . $item['client_id'] . ' AND msa_prestationobject.id=' . $list[$key]['prestation_id']);
            $list[$key]['agenda_id'] = ' ';
            $list[$key]['client_id'] = ' ';
            $list[$key]['prestation_id'] = ' ';
            if (Erra_Core_DatabasePdo::rowCount()) {
                $infos = $trashLine->fetch();
                $list[$key]['agenda_id'] = $infos->nom_agenda;
                $list[$key]['client_id'] = $infos->nom . ' ' . $infos->prenom;
                $list[$key]['prestation_id'] = $infos->prestation_name;
            }
            $image_retore = config_get('framework_images') . '/cmsimages/restore.png';
            $list[$key]['slot_day'] = time_to_euro_date($list[$key]['slot_day']);
            $list[$key]['slot_start'] = time_to_str_hour($list[$key]['slot_start']);
            $list[$key]['event_id'] = '<span id="' . $list[$key]['event_id'] . '" class="live-click pointer restore-item" data-function="restore_behaviour"><img src="' . $image_retore . '"/></span>';
        }
        $this->setObjectDataArray($list);
    }

}

?>
